﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading;
using ZD_Utility;
using System.Net;
using System.Text;
using System.Collections.Specialized;

public partial class _Default : BasePage
{

    string body = "postToken=$.postToken&src=$.src&timestamp=$.timestamp&referpage=$.referpage&plgs=$.plgs&fonts=$.fonts&loadTimer=$.loadTimer&category=$.category&category=$.category&category=$.category&wanted=$.wanted&faburen=$.faburen&地区_area2=$.地区_area2&地区_area2=$.地区_area2&地区_area3=$.地区_area3&地区_area3=$.地区_area3&具体地点=$.具体地点&具体地点=$.具体地点&建造年代=$.建造年代&物业类型=$.物业类型&装修情况=$.装修情况&朝向=$.朝向&房型=$.房型&房型厅=$.房型厅&房型卫=$.房型卫&面积=$.面积&楼层=$.楼层&看房时间=$.看房时间&价格=$.价格&附近交通=$.附近交通&联系人=$.联系人&contact=$.contact&title=$.title&description=$.description ";

    string postData = string.Empty;
    //发布信息实体
    Information model;
    string faburen = "m33663"; //个人：m33660，经济人：m33663
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            return;
        //DataTable dt = new DataTable();
        //dt.Columns.Add("name", typeof(string));
        //dt.Columns.Add("value", typeof(string));
        //postData = @"postToken={0}&src=header&timestamp={1}&referpage=http://shanghai.baixing.com/&plgs=&fonts=&loadTimer=10&category=&category=&category=ershoufang&wanted=0&faburen=m33660&地区_area2=浦东&地区_area2=m7255&地区_area3=外高桥&地区_area3=m2249&具体地点=888号&具体地点=888号&建造年代=1990年&物业类型=普通住宅&装修情况=豪华装修&朝向=东西&房型=3室&房型厅=2厅&房型卫=2卫&面积=123平米&楼层=3/12&看房时间=随时看房&价格=380万元&附近交通=&联系人=李总&contact=13661964923&title=浦东外高桥 2010 3室2厅 123平米&description=房间是新装修的，3室2厅2卫，这个价格是在是他便宜了，不要错过哦！！！";
        ////var arr = postData.Split('&');
        ////List<string> list = new List<string>();
        ////foreach (string item in arr)
        ////{
        ////    string[] arr1 = item.Split('=');
        ////    Response.Write(arr1[0] + "：" + arr1[1] + "<hr/>");
        ////    list.Add(string.Join("=", arr1));
        ////}
        ////Response.Write(string.Join("&", list));
        ////return;
    }


    public string PostInfo()
    {

        string loginData = "identity={0}&userid=&password={1}&action=login&redirect=http%3A%2F%2Fwww.baixing.com%2Fwo%2Fprofile%2F&refer=";
        HttpPost a = new HttpPost("http://www.baixing.com/auth/denglu?from=&redirect=http%3A%2F%2Fwww.baixing.com%2Fwo%2Fprofile%2F", string.Format(loginData, model.CellPhone, model.Password));
        a.req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        a.req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1";
        a.req.Referer = "http://www.baixing.com/auth/denglu?from=&redirect=http%3A%2F%2Fwww.baixing.com%2Fwo%2Fprofile%2F";
        a.req.KeepAlive = true;
        a.req.Host = "www.baixing.com";
        a.req.ContentType = "application/x-www-form-urlencoded";

        CookieCollection aa = new CookieCollection();
        aa.Add(new Cookie("__trackId", "130871216076576", "/", "www.baixing.com"));
        aa.Add(new Cookie("__utma", "63213232.1100146787.1308712166.1342090414.1342178881.13", "/", "www.baixing.com"));
        aa.Add(new Cookie("__uuid", "113087122725396.910a6", "/", "www.baixing.com"));
        aa.Add(new Cookie("__utmz", "63213232.1337606162.10.1.utmcsr=baidu|utmccn=(organic)|utmcmd=organic|utmctr=%B0%D9%D0%D5%CD%F8", "/", "www.baixing.com"));
        aa.Add(new Cookie("__city", "shanghai", "/", "www.baixing.com"));
        aa.Add(new Cookie("__utmb", "63213232.8.9.1342178916626", "/", "www.baixing.com"));
        aa.Add(new Cookie("__utmc", "63213232", "/", "www.baixing.com"));
        aa.Add(new Cookie("bx.guide", "256", "/", "www.baixing.com"));
        aa.Add(new Cookie("bx.guide.time", "1342178886937", "/", "www.baixing.com"));

        CookieContainer container = new CookieContainer();
        container.Add(aa);
        a.req.CookieContainer = container;

        string str = a.SendRequest();
        //登陆cookie
        CookieContainer loginCookie = a.req.CookieContainer;

        //获取token
        HttpGet get = new HttpGet("http://www.baixing.com/fabu/?src=header&city=shanghai");
        get.req.CookieContainer = loginCookie;
        get.req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        get.req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1";
        get.req.KeepAlive = true;
        get.req.Host = "www.baixing.com";
        str = get.SendRequest();

        //<input type="hidden" name="postToken" value="f1401d2801195b21f81c79d1377368ad" />
        //<input type="hidden" name="timestamp" value="1342255034" />
        Regex reg = new Regex("(?is)<input[^>]*?name=\"postToken\"\\svalue=\"(?<value>[^>]*?)\"\\s/>");
        Regex reg1 = new Regex("(?is)<input[^>]*?name=\"timestamp\"\\svalue=\"(?<value>[^>]*?)\"\\s/>");
        string token = reg.Match(str).Groups["value"].Value;
        string timestamp = reg1.Match(str).Groups["value"].Value;

        //发布
        string postdata = string.Format(postData, token, timestamp, model.CellPhone);
        postdata = Server.UrlEncode(postdata).Replace("%3d", "=").Replace("%26", "&");
        HttpPost post = new HttpPost("http://www.baixing.com/fabu/post/?city=shanghai", postdata);
        post.req.CookieContainer = loginCookie;
        post.req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        post.req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1";
        post.req.KeepAlive = true;
        post.req.Host = "www.baixing.com";
        post.req.Referer = " http://www.baixing.com/fabu/?src=header&city=shanghai";
        post.req.ContentType = "application/x-www-form-urlencoded";
        str = post.SendRequest();

        return str;
    }



    protected void Btn_Post_Click(object sender, EventArgs e)
    {
        //postData = @"postToken={0}&src=header&timestamp={1}&referpage=http://shanghai.baixing.com/&plgs=&fonts=&loadTimer=10,10&wanted=1&faburen=m33660&地区_area2=黄浦,m7264&具体地点=,888号&建造年代=1991年&物业类型=普通住宅&装修情况=简单装修&朝向=西&房型=3室&房型厅=1厅&房型卫=2卫&面积=150&楼层=3/10&有无电梯[]=有电梯,已满5年&看房时间=随时看房&周边配套[]=幼儿园,中小学,超市,公交地铁&房源特色[]=地铁沿线,大型社区,小户型&价格=150&附近交通=3号线，1号线&联系人=刘总&contact={2}&title=黄埔好房子快进来！！！&description=有房子的，速度约起来哦。时间不等人111！！！&category=&category=&category=ershoufang";
        List<string> list = new List<string>();
        int i = 0;
        foreach (string item in Request.Form.Keys)
        {
            if (i > 3 && i != Request.Form.Count - 1)
            {
                if (item == "地区_area2" || item == "地区_area3" || item == "具体地点")
                {
                    list.Add(string.Format("{0}={1}", item, Request.Form[item].Split(',')[0]));
                    list.Add(string.Format("{0}={1}", item, Request.Form[item].Split(',')[1]));
                }
                else
                    list.Add(string.Format("{0}={1}", item, Request.Form[item]));
            }
            i++;
        }
        postData = string.Join("&", list);
        postData += "&category=&category=&category=ershoufang";
        model = new Information
              {
                  UserName = this.Txt_UserName.Text,
                  Password = this.Txt_PassWord.Text,
                  CellPhone = this.Txt_CellPhone.Text
              };
        Response.Write(PostInfo());

    }


    #region 编码
    public string UTF8ToGB2312(string str)
    {
        try
        {
            Encoding utf8 = Encoding.GetEncoding(65001);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            byte[] temp = utf8.GetBytes(str);
            byte[] temp1 = Encoding.Convert(utf8, gb2312, temp);
            string result = gb2312.GetString(temp1);
            return result;
        }
        catch
        {
            return null;
        }
    }

    public string GB2312ToUTF8(string str)
    {
        try
        {
            Encoding uft8 = Encoding.GetEncoding(65001);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            byte[] temp = gb2312.GetBytes(str);
            byte[] temp1 = Encoding.Convert(gb2312, uft8, temp);
            string result = uft8.GetString(temp1);
            return result;
        }
        catch
        { return null; }
    }
    #endregion

}



/// <summary>
/// 发布信息实体
/// </summary>
public class Information
{
    public Information()
    {
    }

    public string UserName { get; set; }
    public string Password { get; set; }
    public string CellPhone { get; set; }
}